//
//  ActivitiesEntranceVC.m
//  YZZX
//
//  Created by Easaa on 15/6/9.
//  Copyright (c) 2015年 Easaa. All rights reserved.
//

#import "ActivitiesEntranceVC.h"

@interface ActivitiesEntranceVC ()<UITextFieldDelegate,HXHttpRequestDelegate,UIAlertViewDelegate>
{
    UIButton  *_ActivitiesBut;
    UITextField  *_Namex;
    UITextField  *_Phonex;
    UIControl  * butcontrol0;
    UIControl  * butcontrol1;
    
    HXHttpRequest  *_httpRequest;
}

@end

@implementation ActivitiesEntranceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"活动报名";
    self.view.backgroundColor = RGBCOLOR(233, 233, 233);
    
    
    UIView  *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 15, Dev_ScreenWidth, 120)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    
    
    butcontrol0 =[[ UIControl alloc]initWithFrame:CGRectMake(0, 0, Dev_ScreenWidth/2, 40)];
    butcontrol0.backgroundColor =[UIColor whiteColor];
    butcontrol0.tag = 0;
    [butcontrol0 addTarget:self action:@selector(ControlFontion:) forControlEvents:UIControlEventTouchUpInside];
    [butcontrol0 setSelected:NO];
    [bgView addSubview:butcontrol0];
  
    //UIControlStateHighlighted
    
    UIImageView  *iamgeViewS =[[UIImageView alloc]initWithFrame:CGRectMake((butcontrol0.frame.size.width-15)/2, 10, 15, 20)];
    iamgeViewS.image =[UIImage imageNamed:@"Sex_man@2x.png"];
    [butcontrol0 addSubview:iamgeViewS];
    

    
    butcontrol1 =[[ UIControl alloc]initWithFrame:CGRectMake(Dev_ScreenWidth/2, 0, Dev_ScreenWidth/2, 40)];
    butcontrol1.backgroundColor =[UIColor whiteColor];
    butcontrol1.tag = 1;
    [butcontrol1 addTarget:self action:@selector(ControlFontion:) forControlEvents:UIControlEventTouchUpInside];
    [butcontrol1 setSelected:NO];
    [bgView addSubview:butcontrol1];
    
    UIImageView  *iamgeView1 =[[UIImageView alloc]initWithFrame:CGRectMake((butcontrol0.frame.size.width-15)/2, 10, 15, 20)];
    iamgeView1.image =[UIImage imageNamed:@"Sex_girl@2x.png"];
    [butcontrol1 addSubview:iamgeView1];

    
    
    UIView  *LineView0 = [[UIView alloc]initWithFrame:CGRectMake(Dev_ScreenWidth/2-1, 0, 1, 40)];
    LineView0.backgroundColor = RGBCOLOR(229, 229, 229);
    [bgView addSubview:LineView0];
    
    
    UIView  *LineView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 40, Dev_ScreenWidth, 1)];
    LineView1.backgroundColor = RGBCOLOR(229, 229, 229);
    [bgView addSubview:LineView1];
    
    
    UIView  *LineView2 = [[UIView alloc]initWithFrame:CGRectMake(10, 80, Dev_ScreenWidth, 1)];
    LineView2.backgroundColor = RGBCOLOR(229, 229, 229);
    [bgView addSubview:LineView2];
    
    
    
    UILabel *labelname = [[UILabel alloc] initWithFrame:CGRectMake(10, 40,40, 40)];
    labelname.font = [UIFont boldSystemFontOfSize:15];           //UILabel的字体大小
    labelname.numberOfLines = 0;                                 //必须定义这个属性，否则UILabel不会换行
    labelname.textAlignment = NSTextAlignmentLeft;               //文本对齐方式
    labelname.textColor = [UIColor grayColor];
    labelname.text =@"姓名";
    [bgView addSubview:labelname];

    
    _Namex =[[UITextField alloc]initWithFrame:CGRectMake(50, 40, Dev_ScreenWidth-60, 40)];
    // _passWordText.delegate =self;
    _Namex.tag =0;
    _Namex.delegate = self;
    _Namex.font = [UIFont systemFontOfSize:15];
//    _Namex.secureTextEntry = YES;  //密文
 //   _Namex.backgroundColor = [UIColor greenColor];
    [bgView addSubview:_Namex];
    

    
    
    UILabel *labelphone = [[UILabel alloc] initWithFrame:CGRectMake(10, 80,40, 40)];
    labelphone.font = [UIFont boldSystemFontOfSize:15];           //UILabel的字体大小
    labelphone.numberOfLines = 0;                                 //必须定义这个属性，否则UILabel不会换行
    labelphone.textAlignment = NSTextAlignmentLeft;               //文本对齐方式
    labelphone.textColor = [UIColor grayColor];
    labelphone.text =@"手机";
    [bgView addSubview:labelphone];
    


    _Phonex =[[UITextField alloc]initWithFrame:CGRectMake(50, 80, Dev_ScreenWidth-60, 40)];
    // _passWordText.delegate =self;
    _Phonex.tag =0;
    _Phonex.delegate = self;
    _Phonex.font = [UIFont systemFontOfSize:15];
    //    _Namex.secureTextEntry = YES;  //密文
    // _Phonex.backgroundColor = [UIColor greenColor];
    [bgView addSubview:_Phonex];

    
    
    
    _ActivitiesBut = [UIButton buttonWithType:UIButtonTypeCustom];
    _ActivitiesBut.tag = 2014 ;
    _ActivitiesBut.frame =CGRectMake(10,bgView.bottom+15 ,Dev_ScreenWidth-20, 40);
    [_ActivitiesBut addTarget:self action:@selector(RequestHeadlActivitiesFantion) forControlEvents:UIControlEventTouchUpInside];
    [_ActivitiesBut.layer setMasksToBounds:YES];
    [_ActivitiesBut.layer setCornerRadius:2]; //设置矩圆角半径
    [_ActivitiesBut.layer setBorderWidth:0];   //边框宽度
    [_ActivitiesBut setTitle:@"报名参加" forState:UIControlStateNormal];
    [_ActivitiesBut setTintColor:[UIColor whiteColor]];
    _ActivitiesBut.backgroundColor =RGBCOLOR(252, 78, 125);
    [self.view addSubview:_ActivitiesBut];

 
    
    

}

- (void)ControlFontion:(UIControl *)contorl
{
    [butcontrol0 setSelected:NO];
    [butcontrol1 setSelected:NO];
    [contorl setSelected:YES];
    
    if(butcontrol0.selected)
    {
        butcontrol0.backgroundColor = RGBCOLOR(214, 214, 214);
    
    }else if(!butcontrol0.selected)
    {
        butcontrol0.backgroundColor = [UIColor whiteColor];
    }
    
    if(butcontrol1.selected)
    {
        butcontrol1.backgroundColor = RGBCOLOR(214, 214, 214);
        
    }else if(!butcontrol1.selected)
    {
        butcontrol1.backgroundColor = [UIColor whiteColor];
    }

}


- (void)RequestHeadlActivitiesFantion
{

    if(!butcontrol1.selected && !butcontrol0.selected)
    {
    
        UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:@"提示"
                                                            message:@"请选择性别！"
                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        
        AlertView.tag = 1023;
        [AlertView show];

        return;
    }
    


    if(_Namex.text.length>0&&_Phonex.text.length>0)
    {
    //        用户名：DeviceApiTester
    //        密码：c8z4mb78

        
    
        NSInteger  sexcount;

        if(butcontrol0.selected ==YES)
        {
            sexcount = 0;
        }else if (butcontrol1.selected==YES)
        {
            sexcount = 1;
        }
        
        
        NSDictionary *Dickey = [NSDictionary dictionaryWithObjectsAndKeys:
                                _Namex.text,    @"name",
                                _Phonex.text,   @"mobile",
                                [NSNumber numberWithInteger:sexcount], @"sex",
                                nil];
        
        //列表请求
        NSDictionary* dicParams = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [XSZ_UICommon useridChangUaerdfLoginBoolString],    @"user_id",
                                   @"1",                @"site_id",
                                   @"articles",         @"app",
                                   @"article",          @"model",
                                   self.CreatID,        @"object_id",
                                   Dickey,              @"post_data",
                                   nil];
        
        
        if (_httpRequest == nil)
        {
            _httpRequest = [[HXHttpRequest alloc] init];
            _httpRequest.delegate = self;
            
            
        }
        [_httpRequest requestForParams:dicParams andMethod:YZ_APPLY_POST andTag:1001];//@"system.time"];//@"category.list"];
        


        }else
        {

            
//            "user_id": "1",
//            "site_id": "1",
//            "app": "{{ app }}",
//            "model": "{{ model }}",
//            "object_id": "{{ object_id }}",
//            "post_data":
//            {
//                "name": "{{ name }}",
//                "sex": {{ sex }},
//                "mobile": "{{ mobile }}"
//            }
//        }

        UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:@"提示"
                                                            message:@"请输入完整！"
                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];

        AlertView.tag = 1024;
        [AlertView show];


        }

}


#pragma mark - HXHttpRequestDelegate  代理

- (void)requestFinished:(NSString *)responseString andStatusCode:(int)statusCode andTag:(NSInteger)tag
{
    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    //tableview列表数据
    if(tag == 1001)
    {
        //  NSLog(@"---------------->>>> %@",responseString);
        
        NSDictionary *dic = [[NSDictionary alloc]init];
        dic = [JsonUtil jsonToDic:responseString];
        
        // NSLog(@"---------------->>>> \n%@",dic);
        
        
        if([[dic objectForKey:@"is_success"] boolValue])
        {
            UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:@"提示"
                                                              message:[dic objectForKey:@"message"]
                                                             delegate:self
                                                    cancelButtonTitle:@"确定"
                                                    otherButtonTitles:nil];
            
            AlertView.tag = 1020;
            [AlertView show];
        
        }else
        {
            UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:@"提示"
                                                                message:[dic objectForKey:@"message"]
                                                               delegate:self
                                                      cancelButtonTitle:@"确定"
                                                      otherButtonTitles:nil];
            
            AlertView.tag = 1025;
            [AlertView show];

        }

    }
    
    
}

- (void)requestFailed:(NSError *)error andTag:(NSInteger)tag
{
    
    
    
    
}



#pragma mark alertView 弹框代理
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if(alertView.tag == 1020)
    {
        if(buttonIndex == 0)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }


}


- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
    [theTextField resignFirstResponder];
    
    return YES;
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


@end



